<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;

class NewsController extends AdminBaseController
{

    public function index(){

        $keyname = input('checkname');
        $where = [];
        if ($keyname){
            $where[] = ['title|author', 'like','%'.$keyname.'%' ];
        }
        $data = NewsModel::order('list_order','DESC')
            ->paginate(6);
        foreach ($data as $news){
            $content1 = htmlspecialchars_decode($news['content']);
            $content2 = cmf_replace_content_file_url($content1,true);
            $news['content'] = $content2;
        }
        $page = $data->render();
        return $this->fetch('index',[
            'news' => $data,
            'page' => $page,
        ]);
    }

    public function toggleSwitch()
    {
        $id = $this->request->param('id', 1, 'intval');
        $news = NewsModel::find($id);
        if ($news) {
            $news->switch = $news->switch ? 0 : 1;
            if ($news->save()) {
                return json(['status' => 1, 'msg' => '状态切换成功', 'switch' => $news->switch]);
            } else {
                return json(['status' => 0, 'msg' => '状态切换失败']);
            }
        } else {
            return json(['status' => 0, 'msg' => '新闻不存在']);
        }
    }

    public function add(){

        return $this->fetch();
    }

    public function addPost(){
        $data = input('post.');
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1,true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        $validate = new NewsValidate();
        $data['create_time'] = date('Y-m-d H:i:s');
        $res = $validate->scene('add')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }else{
            if (NewsModel::create($data)){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }

        }
    }

    public function edit(){
        $id = input('id/d');
        $data = NewsModel::find($id);
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1,true);
        $data['content']= $content2;
        return $this->fetch('edit',[
            'news' => $data,
        ]);
    }

    public function editPost(){
        if ($this->request->isPost()){
            $data = $this->request->post();

            $validate = new NewsValidate();
            $res = $validate->scene('add')->check($data);
            if (!$res){
                $this->error($validate->getError());
            }

            $news = NewsModel::find($data['id']);
            if (!$news){
                    $this->error('新闻不存在');
            }
            $content1 = htmlspecialchars_decode($data['content']);
            $content2 = cmf_replace_content_file_url($content1,true);
            $content3 = htmlspecialchars($content2);
            $data['content']= $content3;

            $news->title = $data['title'];
            $news->author = $data['author'];
            $news->look_num = $data['look_num'];
            $news->url = $data['url'];
            $news->image = $data['image'];
            $news->update_time = date('Y-m-d H:i:s');
            $news->content = $data['content'];
            if ($news->save()){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }

    }
    public function delete(){
        $id = input('id');
        if (!$id){
            $this->error('该数据不存在');
        }else{
            if (NewsModel::destroy($id)){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
    }

    public function order(){
        parent::listOrders(new NewsModel());
        $this->success('排序成功');
    }

    public function content(){
        $id=input('id');
        $content=NewsModel::find($id);
        $content1= cmf_replace_content_file_url($content['content'],true);
        $content['content'] = $content1;
        return $this->fetch('content',[
            'article' =>$content
        ]);
    }

    public function totolgSwitch(){
        $id = input('id/d');
        $news = NewsModel::find($id);

        if (!$news) {
            $this->error('新闻不存在');
        }

        // 切换状态
        $news->switch = $news->switch ? 0 : 1;
        if ($news->save()) {
            $this->success('切换成功');
        } else {
            $this->error('切换失败');
        }
    }
}